package client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class UserClient implements  ActionListener{
    JFrame jf;
    JPanel jp;
    JLabel label_name;
    JLabel label_result;

    JTextField operation;
    JButton jb;

    JLabel hintStr;
    JLabel resultLabel;

    public UserClient(){
        jf=new JFrame("个人通信薄");
        jp=new JPanel();

        jf.setContentPane(jp);
        jf.setPreferredSize(new Dimension(970,520));

        jp.setPreferredSize(new Dimension(510,520));
        jp.setBackground(Color.white);

        label_name=new JLabel();
        label_name.setPreferredSize(new Dimension(125,30));
        label_name.setText("请输入命令及参数");

        operation=new JTextField();
        operation.setPreferredSize(new Dimension(500,30));


        jp.add(label_name);
        jp.add(operation);




        jb=new JButton("OK");
        jb.setPreferredSize(new Dimension(120,30));
        jb.setText("确  定");
        jb.addActionListener( this);
        jp.add(jb);
        hintStr=new JLabel();
        hintStr.setPreferredSize(new Dimension(900,30));
        hintStr.setText("");
        hintStr.setForeground(Color.RED);
        jp.add(hintStr);

        resultLabel= new JLabel();
        resultLabel.setPreferredSize(new Dimension(900,400));
        resultLabel.setText("");
        jp.add(resultLabel);

        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private String operationConstraint;

    @Override
    public void actionPerformed(ActionEvent e) {
        operationConstraint = operation.getText();

        if(operationConstraint!=null &&  !operationConstraint.equals("")){
            hintStr.setText("正在处理，请稍候...");
            start();
        }else{
            hintStr.setText("请输入操作及条件!");
        }
    }

    OutputStream os;
    Socket s;
    InputStream is;
    public void start(){
        //建立联网线程
        new Thread(new Runnable(){
            public void run() {
                try {

                    s=new Socket("127.0.0.1",8888);
                    //写
                    os=s.getOutputStream();
                    os.write(operationConstraint.getBytes());
                    os.flush();
                    //读内容
                    Thread.sleep(1000);
                    is=s.getInputStream();
                    int len=is.available();
                    System.out.println("len:"+len);
                    byte[] bytes=new byte[len];
                    is.read(bytes);
                    String resut=new String(bytes);
                    System.out.println("resut:"+resut);
                    //TODO 这里通过返回结果处理
                    resultLabel.setText(resut);
                    hintStr.setText("");

                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally{
//                     try {
//                      os.close();
//                      is.close();
//                      s.close();
//                     } catch (IOException e) {
//                      e.printStackTrace();
//                     }
                }
            }
        }).start();
    }


}